package com.wswy.wzcx.ui.car.choose

import android.app.Activity
import android.arch.lifecycle.Observer
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.Log
import android.view.View
import com.che.libcommon.ui.BaseFragment
import com.che.libcommon.ui.NavTabController
import com.wswy.wzcx.R
import com.wswy.wzcx.model.car.CarTypeData
import com.wswy.wzcx.module.base.CBaseActivity
import com.wswy.wzcx.ui.Constants

class ChooseCarActivity : CBaseActivity(),NavTabController.FragmentProvider {


    companion object {
        private const val TAG = "ChooseCarActivity"

        private const val CHOOSED_CAR = "car"

        private const val TAG_BRANDS = "brands"

        private const val TAG_MODELS = "models"


        fun getStartIntent(context: Context,carTypeData: CarTypeData?):Intent = Intent(context,ChooseCarActivity::class.java).apply {
            carTypeData?.let {
                putExtra(CHOOSED_CAR,it)
            }
        }

    }

    private lateinit var tabController: NavTabController
    private lateinit var chooseCarViewModel : ChooseCarViewModel

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_choose_car)



        tabController = NavTabController(this,supportFragmentManager,R.id.fl_container)



        tabController.setOnChangeCallback {
            when(it){
                TAG_BRANDS -> {
                    setAppTitle("选择车系")
                }
                TAG_MODELS -> {
                    setAppTitle("选择车型")
                }
            }
        }


        chooseCarViewModel = provideViewModel(ChooseCarViewModel::class.java)

        chooseCarViewModel.getCurrentSeriesLiveData().observe(this, Observer {

            it?.run {
                tabController.show(TAG_MODELS)
            }

        })


        intent.getParcelableExtra<CarTypeData?>(CHOOSED_CAR)?.run {

            series?.id?.let {
                chooseCarViewModel.chooseCar.brand = brand
                chooseCarViewModel.chooseCar.series = series
                chooseCarViewModel.chooseCar.model = model
                tabController.show(TAG_MODELS)
                chooseCarViewModel.setSeries(it)
            }?:kotlin.run {
                tabController.show(TAG_BRANDS)
            }

        }?:kotlin.run {
            tabController.show(TAG_BRANDS)
        }






    }


    override fun getFragment(tag: String?): Fragment {
        return when(tag){
            TAG_BRANDS -> ChooseBrandFragment()
            TAG_MODELS -> ChooseModelFragment()
            else -> ChooseBrandFragment()
        }
    }

    override fun onBackPressed() {

        var handle = false
        if (tabController.isShowing(TAG_BRANDS)){

            (tabController.currentFragment as? BaseFragment)?.let {
                handle = it.onBackPress()
            }
        }else if (tabController.isShowing(TAG_MODELS)){
            tabController.show(TAG_BRANDS)

            handle = true
        }

        if (!handle){
            setResult(Activity.RESULT_OK, Intent().putExtra(Constants.EXTRA_DATA_CHOOSE_CAR,chooseCarViewModel.chooseCar))
            super.onBackPressed()
        }
    }

    override fun onClickBack(view: View) {
        setResult(Activity.RESULT_OK, Intent().putExtra(Constants.EXTRA_DATA_CHOOSE_CAR,chooseCarViewModel.chooseCar))
        super.onClickBack(view)
    }

}